<?php
class daka_class_testAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('daka_class_test');
		$this->_name = $this->getActionName();
    }
     public function _before_index() {
       
        //$this->list_relation = true;
    }
	//列表页
    public function index() {
        $sort = $this->_request("sort", 'trim', 'ordid');
        $order = $this->_request("order", 'trim', 'ASC');
        $mod_pk = $this->_mod->getPk();
        $pid=$this->_get('pid','intval',0);
        $class_id=$this->_get('class_list_id','intval',0);
        $map=array();
        if($pid)$map['pid']=$pid;
        if($class_id){
			$map['class_list_id']=$class_id;
		}else{
			//查询出所有属于  讲座详情 并且开启的 id
			$data_li = D('daka_class_list')->where(array('type'=>1,'status'=>1))->select();
			$ids = '';
			if($data_li){
				
				foreach($data_li as $k=>$v){
					$ids .= $v['id'].',';
				}
				
				$ids = substr($ids,0,-1);
			}else{
				$ids = 0;
			}
			
			$map['class_list_id']=array('in',$ids);
		}
		
		//如果需要分页
		$pagesize =C('zyb_class_test_pagesize')?C('zyb_class_test_pagesize'):20;
		$count = $this->_mod->where($map)->count($mod_pk);
		$pager = new Page($count, $pagesize);
		$list = $this->_mod->where($map)->order($sort . ' ' . $order)->relation(true)->limit($pager->firstRow.','.$pager->listRows)->select();
		$page = $pager->show();
		$this->assign("page", $page);
        $this->assign('list', $list);
        $this->assign('pid',$pid);
        $this->display();
    }

    public function _before_add() {
        $class_list = M('daka_class_list')->where('status=1')->select();
        $this->assign('class_list', $class_list);
    }
	
    
	public function _before_insert($data='') {
		$data['content'] = json_encode($data['content']);
        return $data;
    }

    public function _before_edit() {
        $this->_before_add();
    }

    public function _before_update($data=''){
        $data['content'] = json_encode($data['content']);
        return $data;
    }

}